package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnProjectUserBo;
import com.ruoyi.system.domain.vo.KfnProjectUserVo;
import com.ruoyi.system.domain.KfnProjectUser;
import com.ruoyi.system.mapper.KfnProjectUserMapper;
import com.ruoyi.system.service.IKfnProjectUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目下的用户Service业务层处理
 *
 * @author Panda
 * @date 2023-12-21
 */
@RequiredArgsConstructor
@Service
public class KfnProjectUserServiceImpl implements IKfnProjectUserService {

    private final KfnProjectUserMapper baseMapper;
    private final SysUserMapper sysUserMapper;

    /**
     * 查询项目下的用户
     */
    @Override
    public KfnProjectUserVo queryById(Long projectUserId){
        return baseMapper.selectVoById(projectUserId);
    }

    /**
     * 查询项目下的用户列表
     */
    @Override
    public TableDataInfo<KfnProjectUserVo> queryPageList(KfnProjectUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnProjectUser> lqw = buildQueryWrapper(bo);
        Page<KfnProjectUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目下的用户列表
     */
    @Override
    public List<KfnProjectUserVo> queryList(KfnProjectUserBo bo) {
        LambdaQueryWrapper<KfnProjectUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KfnProjectUser> buildQueryWrapper(KfnProjectUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnProjectUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProjectUserRole()), KfnProjectUser::getProjectUserRole, bo.getProjectUserRole());
        lqw.eq(bo.getUserId() != null, KfnProjectUser::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectUserStatus()), KfnProjectUser::getProjectUserStatus, bo.getProjectUserStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), KfnProjectUser::getProjectId, bo.getProjectId());
        return lqw;
    }

    /**
     * 新增项目下的用户
     */
    @Override
    public Boolean insertByBo(KfnProjectUserBo bo) {
        KfnProjectUser add = BeanUtil.toBean(bo, KfnProjectUser.class);
        validEntityBeforeSave(add);

        //用户id换用户名，并存入数据库
        SysUser sysUser = sysUserMapper.selectUserById(bo.getUserId());
        add.setUserName(sysUser.getUserName());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectUserId(add.getProjectUserId());
        }
        return flag;
    }

    /**
     * 修改项目下的用户
     */
    @Override
    public Boolean updateByBo(KfnProjectUserBo bo) {
        KfnProjectUser update = BeanUtil.toBean(bo, KfnProjectUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnProjectUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目下的用户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
